package service

import (
	"fmt"
	"log"
	"time"

	"gitee.com/cheng470/openai/util"
	asr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/asr/v20190614"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
)

type TxAsrService struct {
	downloadUrl string
	secretId    string
	secretKey   string
}

func NewTxAsrService(host, port, secretId, secretKey string) *TxAsrService {
	return &TxAsrService{
		downloadUrl: "http://" + host + ":" + port + "/download?file=",
		secretId:    secretId,
		secretKey:   secretKey,
	}
}

func (t *TxAsrService) HandleSentenceRecognition(filename, format string) (string, error) {
	credential := common.NewCredential(t.secretId, t.secretKey)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "asr.tencentcloudapi.com"
	client, _ := asr.NewClient(credential, "", cpf)

	request := asr.NewSentenceRecognitionRequest()

	request.ProjectId = common.Uint64Ptr(0)
	request.SubServiceType = common.Uint64Ptr(2)
	request.EngSerViceType = common.StringPtr("16k_zh")
	request.SourceType = common.Uint64Ptr(0)
	request.Url = common.StringPtr(t.downloadUrl + filename)
	request.VoiceFormat = common.StringPtr(format)
	request.UsrAudioKey = common.StringPtr("testasdf" + filename)

	s := time.Now()
	response, err := client.SentenceRecognition(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		log.Printf("An API error has returned: %s", err)
		return fmt.Sprintf("An API error has returned: %s", err), err
	}
	if err != nil {
		log.Printf("An API error has returned: %s", err)
		return fmt.Sprintf("An API error has returned: %s", err), err
	}
	fmt.Printf("请求：%s\n响应： %s\n耗时: %d毫秒\n", util.JsonEncode(request), util.JsonEncode(response), time.Now().UnixMilli()-s.UnixMilli())
	return *response.Response.Result, nil
}

func (t *TxAsrService) HandleSentenceRecognition2(filename, format string) (response *asr.SentenceRecognitionResponse, err error) {
	credential := common.NewCredential(t.secretId, t.secretKey)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "asr.tencentcloudapi.com"
	client, _ := asr.NewClient(credential, "", cpf)

	request := asr.NewSentenceRecognitionRequest()

	request.ProjectId = common.Uint64Ptr(0)
	request.SubServiceType = common.Uint64Ptr(2)
	request.EngSerViceType = common.StringPtr("16k_zh")
	request.SourceType = common.Uint64Ptr(0)
	request.Url = common.StringPtr(t.downloadUrl + filename)
	request.VoiceFormat = common.StringPtr(format)
	request.UsrAudioKey = common.StringPtr("testasdf" + filename)

	s := time.Now()
	response, err = client.SentenceRecognition(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		log.Printf("An API error has returned: %s", err)
		return
	}
	if err != nil {
		log.Printf("An API error has returned: %s", err)
		return
	}
	fmt.Printf("请求：%s\n响应： %s\n耗时: %d纳秒\n", util.JsonEncode(request), util.JsonEncode(response), time.Now().UnixNano()-s.UnixNano())
	return
}
